package org.dk.poitalk.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.jdo.annotations.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.dk.poitalk.dao.GenericDao;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.memcache.MemcacheService;

public class GenericDaoImpl<T, PK> implements GenericDao<T, Serializable> {

	private static final Logger logger = Logger.getLogger(GenericDaoImpl.class
			.getName());
	
	@PersistenceContext
	protected transient EntityManager entityManager;

	/**
	 * The datastore service.
	 */
	private DatastoreService datastoreService;

	private MemcacheService memcacheService;

	/**
	 * Returns the datastore service.
	 * 
	 * @return the datastore service
	 */
	public final DatastoreService getDatastoreService() {
		return datastoreService;
	}

	/**
	 * Sets the datastore service.
	 * 
	 * @param datastoreService
	 *            the datastore service
	 */
	public final void setDatastoreService(
			final DatastoreService datastoreService) {
		this.datastoreService = datastoreService;
	}

	/**
	 * Returns the memcache service.
	 * 
	 * @return the memcache service
	 */
	public final MemcacheService getMemcacheService() {
		return memcacheService;
	}

	/**
	 * Sets the memcache service.
	 * 
	 * @param memcacheService
	 *            the memcache service
	 */
	public final void setMemcacheService(final MemcacheService memcacheService) {
		this.memcacheService = memcacheService;
	}
	

	
	@Override	
	public T find(Class<T> entityClass, Serializable primaryKey) {
		logger.info(String.format("GenericDao %s to find with id %s", entityClass.toString(), primaryKey.toString()));
		@SuppressWarnings("unchecked")
		T rEntityClass = (T) entityManager.find(((Class<T>) entityClass).getClass(), primaryKey);
		logger.info(String.format("GenericDao %s finded with id %s",entityClass, primaryKey.toString()));
		return rEntityClass;
	}

	@Override
	@Transactional
	public void save(T entityClass) {
		// TODO save Auto-generated method stub
		
	}

	@Override
	@Transactional
	public T saveOrUpdate(T entityClass) {
		// TODO saveOrUpdate Auto-generated method stub
		return null;
	}

	@Override
	public int delete(T entityClass) {
		// TODO delete Auto-generated method stub
		return 0;
	}

	@Override
	public List<T> search(Map<String, String> mapValue) {
		// TODO search Auto-generated method stub
		return null;
	}

}
